﻿#include "FITKAcousticsSEAModelAdaptor.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEAFace.h"

#include "FITKAcousticsSEASubSysMgrAdaptor.h"
#include "FITKAcousticsSEAFaceMgrAdaptor.h"

bool IO::FITKAcousticsSEAModelAdaptor::adaptR()
{
    Acoustics::FITKAcousticsSEAModel* SEAModelP =
        dynamic_cast<Acoustics::FITKAcousticsSEAModel*>(_dataObj);
    if (SEAModelP == nullptr) return false;

    Acoustics::FITKAcousticsSEASubSysManager* subSysMangagerP = SEAModelP->getSEASubSysManager();//子系统管理器
    Acoustics::FITKSEAFaceManager* FaceManagerP = SEAModelP->getSEAFaceManager();//Face管理器
    bool subSysMangagerRet = false;
    bool FaceManagerRet = false;
    bool JunctionManagerRet = false;

    if (FaceManagerP != nullptr)
    {
        //Face管理器适配器
        FITKAcousticsSEAFaceMgrAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEAFaceMgrAdaptor>("FITKAcousticsHDF5", FaceManagerP);
        if (adaptor == nullptr) return false;
        adaptor->setReader(_reader);
        adaptor->setDataObject(FaceManagerP);
        FaceManagerRet = adaptor->adaptR();
        delete adaptor;
    }
    if (subSysMangagerP != nullptr)
    {
        //调用子系统管理器适配器
        FITKAcousticsSEASubSysMgrAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEASubSysMgrAdaptor>("FITKAcousticsHDF5", subSysMangagerP);
        if (adaptor == nullptr) return false;
        adaptor->setReader(_reader);
        adaptor->setDataObject(subSysMangagerP);
        subSysMangagerRet = adaptor->adaptR();
        delete adaptor;
    }
    return subSysMangagerRet && FaceManagerRet;
}

bool IO::FITKAcousticsSEAModelAdaptor::adaptW()
{
    Acoustics::FITKAcousticsSEAModel* SEAModelP =
        dynamic_cast<Acoustics::FITKAcousticsSEAModel*>(_dataObj);
    if (SEAModelP == nullptr) return false;


    Acoustics::FITKAcousticsSEASubSysManager* subSysMangagerP = SEAModelP->getSEASubSysManager();//子系统管理器
    Acoustics::FITKSEAFaceManager* FaceManagerP = SEAModelP->getSEAFaceManager();//Face管理器

    bool subSysMangagerRet = false;
    bool FaceManagerRet = false;
    bool JunctionManagerRet = false;

    if (subSysMangagerP != nullptr)
    {
        //调用子系统管理器适配器
        FITKAcousticsSEASubSysMgrAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEASubSysMgrAdaptor>("FITKAcousticsHDF5", subSysMangagerP);
        if (adaptor == nullptr) return false;
        adaptor->setWriter(_writer);
        adaptor->setDataObject(subSysMangagerP);
        subSysMangagerRet = adaptor->adaptW();
        delete adaptor;
    }
    
    if (FaceManagerP != nullptr)
    {
        //Face管理器适配器
        FITKAcousticsSEAFaceMgrAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEAFaceMgrAdaptor>("FITKAcousticsHDF5", FaceManagerP);
        if (adaptor == nullptr) return false;
        adaptor->setWriter(_writer);
        adaptor->setDataObject(FaceManagerP);
        FaceManagerRet = adaptor->adaptW();
        delete adaptor;
    }
    return subSysMangagerRet && FaceManagerRet;
}
